﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Reborn_WorldServer.App1AppHandler
{
    public class QueueTimer
    {
        public QueueTimer() { }
        public QueueTimer(double interval)
        {
            Interval = interval;
        }

        public double Interval = 3000;

        private bool enabled = false;
        private DateTime expires = DateTime.Now;
        private Object lockObject = new Object();

        public void Start()
        {
            lock (lockObject)
            {
                expires = DateTime.Now.AddMilliseconds(Interval);
                enabled = true;
            }
        }

        public void StartWithOffset(double offset)
        {
            lock (lockObject)
            {
                expires = DateTime.Now.AddMilliseconds(Interval + offset);
                enabled = true;
            }
        }
        public bool Stop()
        {
            lock (lockObject)
            {
                if (enabled == true)
                {
                    enabled = false;
                    return true;
                }
            }
            return false;
        }
        public bool timerExpired
        {
            get
            {
                lock (lockObject)
                {
                    if (enabled == true && DateTime.Now >= expires)
                    {
                        enabled = false;
                        return true;
                    }
                }
                return false;
            }
        }

        public void Dispose()
        {

        }
    }
}
